#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""GIT context class

Defines the factory method to get the instance of the GIT related classes.
"""

__author__ = 'Xin Zhou(zhounew@google.com)'

from git.config import config
from git.session import git_gaesession_based_session_manager
from git.util import git_api_client
from git.util import git_dasher_domain_checker
from git.util import git_config

class GitContext:
  git_dasher_domain_checker_instance_ = None
  session_manager_ = None
  config_ = None
  api_client_ = None
  service_impl_ = None
  
  @staticmethod
  def GetAccountService():
    '''Gets the account service defined by the RP site.
    '''
    if not GitContext.service_impl_:
      GitContext.service_impl_ = config.GIT_CONFIG['gitAccountServiceImpl']
    return GitContext.service_impl_
  
  @staticmethod
  def GetSessionManager():
    '''Currently we use Gaesession based session manager.
    You can replace it as what you implemented in your websites
    '''
    if not GitContext.session_manager_:
      return git_gaesession_based_session_manager.GitGaeSessionBasedSessionManager(GitContext.GetConfig(), GitContext.GetAccountService())
    return GitContext.session_manager_
  
  @staticmethod
  def GetDasherDomainChecker():
    '''Gets the dasher domain checker instance.
    '''
    if not GitContext.git_dasher_domain_checker_instance_:
      GitContext.git_dasher_domain_checker_instance_ = \
          git_dasher_domain_checker.GitDasherDomainChecker()
    return GitContext.git_dasher_domain_checker_instance_
  
  @staticmethod
  def GetConfig():
    '''Gets the config instance.
    '''
    if not GitContext.config_:
      GitContext.config_ = git_config.GitConfig(True)
    return GitContext.config_
  
  @staticmethod
  def GetApiClient():
    '''Gets the api client instance.
    '''
    if not GitContext.api_client_:
      api_key = GitContext.GetConfig().GetApiKey()
      GitContext.api_client_ = git_api_client.GitApiClient(api_key)
    return GitContext.api_client_